﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HYSoft.Web
{
    public partial class monitor_add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["uid"] == null || Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
        }
        protected string tounit1()
        {
            string units1 = "";
            Model.user users = new BLL.user().GetModel(int.Parse(Session["uid"].ToString()));
            units1 = users.unit;
            return units1;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            string fileNamee1 = "";
            string fileNamee = FileUpload1.FileName.ToString();
            if (fileNamee == "")
            {
                fileNamee = "monitor.jpg";
                fileNamee1 = "monitorimage/" + fileNamee;
            }
            else
            {
                fileNamee1 = "monitorimage/" + fileNamee;
            }

            string text1 = this.address.Value;
            //string text1 = this.selProvince1.Value.Trim() + this.selCity1.Value.Trim() + this.selDistrict1.Value.Trim();
            //string text1 = this.DropDownList23.SelectedValue.ToString();
            //string text2 = this.TextBox2.Text;
            string text3 = this.TextBox3.Value; 
            string text4 = this.TextBox4.Value;
            string text5 = this.TextBox5.Value;
            string text6 = this.TextBox6.Value;
            string text7 = this.TextBox7.Value;
            string text8 = this.TextBox8.Value;
            string text9 = this.TextBox9.Value;
            string text10 = this.TextBox10.Value;
            string text11 = this.TextBox11.Value;
            string text12 = this.TextBox12.Value;
            string text13 = this.TextBox13.Value;
            string text14 = this.TextBox14.Value;
            string text15 = this.TextBox15.Value;
            string text16 = this.TextBox16.Value;
            string text17 = this.TextBox17.Value;
            string text18 = this.TextBox18.Value;

            string text20 = this.TextBox20.Value;
            string text19 = "";

            string[] strs = text20.ToString().Trim().Split(',');




            string typ3 = fileNamee.Substring(fileNamee.LastIndexOf(".") + 1);//上传文件的后缀名

            typ3 = typ3.ToLower();
            string sql = " Addr='" + text3 + "'";
            DataSet ds = new BLL.monitor().GetList(sql);
            DataTable dt8 = ds.Tables[0];
            if (dt8.Rows.Count > 0)
            {
                dt8.Clear();
                dt8.Dispose();
                Response.Write("<script>alert('该监测点地址已存在，请确认添加监测点信息正确！');history.back();</script>");
            }
            else
            {
                if (strs.Length == 2)
                {
                    text20 = strs[0].ToString();
                    text19 = strs[1].ToString();
                    if (typ3 == "gif" || typ3 == "jpg" || typ3 == "png")
                    {
                        FileUpload1.SaveAs(Server.MapPath("monitorimage/") + fileNamee);
                        Model.monitor monitor = new Model.monitor();
                        monitor.count = 0;
                        monitor.m_area_name = text1;
                        monitor.m_area_id = text1;
                        monitor.addr = text3;
                        monitor.type = text4;
                        monitor.level = text5;
                        int pop = 0;
                        if (text6 != "")
                            pop = int.Parse(text6);
                        monitor.population = pop;
                        monitor.contact_name1 = text7;
                        monitor.contact_mobile1 = text8;
                        monitor.contact_name2 = text9;
                        monitor.contact_mobile2 = text10;
                        monitor.contact_name3 = text11;
                        monitor.contact_mobile3 = text12;
                        monitor.upunit1 = text13;
                        monitor.upcontact_name1 = text14;
                        monitor.upcontact_mobile1 = text15;
                        monitor.upunit2 = text16;
                        monitor.upcontact_name2 = text17;
                        monitor.upcontact_mobile2 = text18;
                        monitor.charvalue1 = fileNamee1;
                        monitor.coordinates_x = text20;
                        monitor.coordinates_y = text19;
                        new BLL.monitor().Add(monitor);
                        string sqls = " Addr='" + text3 + "'";
                        DataSet ds1 = new BLL.monitor().GetList(sqls);
                        DataTable dt1 = ds1.Tables[0];
                        int monitorid = int.Parse(dt1.Rows[0]["index"].ToString());
                        Model.monitor_to_user mtou = new Model.monitor_to_user();
                        mtou.monitor_index = monitorid;
                        mtou.user_index = int.Parse(Session["uid"].ToString());
                        new BLL.monitor_to_user().Add(mtou);
                        Response.Write("<script>alert('监测点添加成功！请分配监测点。');window.location = 'monitorfp.aspx';</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('现场图片未选择！');history.back();</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('地址坐标格式输入错误,请输入两个数值并用逗号隔开！');</script>");
                }

            }

        }
    }
}